package com.jhx.video.discuss

import com.jhx.common.http.ApiManager
import com.jhx.common.http.HttpRequest
import com.jhx.common.http.ResponseCallback
import com.jhx.video.model.VideoDetail
import com.jhx.video.service.VideoService

/**
 *
 *
 * @author Jack 2018-03-28 10:55
 */
class DiscussPresenterImpl : Contract.DiscussPresenter() {

    override fun deleteVideo(id: String) {
        val call = ApiManager.get().getApiService(VideoService::class.java)
                .deleteVideo(id)
        mView?.showLoading()
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(call, object: ResponseCallback<Any>() {
                    override fun onSuccess(url: String?, data: Any?) {
                        mView?.hideLoading()
                        mView?.deleteVideoSuccess()
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        mView?.hideLoading()
                    }
                })
    }

    override fun stickVideo(id: String, isTop: Int) {
        mView?.showLoading()
        val call = ApiManager.get().getApiService(VideoService::class.java)
                .stickVideo(id, isTop)
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(call, object: ResponseCallback<Any>() {
                    override fun onSuccess(url: String?, data: Any?) {
                        mView?.hideLoading()
                        mView?.stickVideoSuccess()
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        mView?.hideLoading()
                    }
                })
    }

    override fun getComment(vid: String, pageIndex: Int, pageSize: Int, onStart: (() -> Unit)?, onComplete: (() -> Unit)?) {
        onStart?.invoke()
        val call = ApiManager.get().getApiService(VideoService::class.java).getComment(vid, pageIndex, pageSize)
        HttpRequest.getRequest().autoRecycle(mView?.getContext()).start(call, object: ResponseCallback<VideoDetail>() {
            override fun onSuccess(url: String?, data: VideoDetail?) {
                onComplete?.invoke()
                mView?.getCommentSuccess(data)
            }

            override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                super.onError(url, errorCode, errorMsg)
                onComplete?.invoke()
            }
        })
    }

    override fun replyComment(id: String, text: String) {
        mView?.showLoading()
        val call = ApiManager.get().getApiService(VideoService::class.java).replyComment(id, text)
        HttpRequest.getRequest().autoRecycle(mView?.getContext()).start(call, object: ResponseCallback<Any>() {
            override fun onSuccess(url: String?, data: Any?) {
                mView?.hideLoading()
                mView?.replyCommentSuccess()
            }

            override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                super.onError(url, errorCode, errorMsg)
                mView?.hideLoading()
            }
        })
    }

    override fun recycler() {

    }

}